//
//  LoginViewController.m
//  LClass
//
//  Created by 严军 on 16/5/29.
//  Copyright © 2016年 YanJun. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "QQRequestManager+handle.h"
#import "RegisterViewController.h"
#import "ChooseViewController.h"
@interface LoginViewController ()
{
    LoginView* loinView;
}
@end

@implementation LoginViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   loinView=[[LoginView alloc]init];
    self.view=loinView;
    
    __block LoginViewController* blockSelf=self;
    
    loinView.loginblock=^(NSDictionary* parDic){

        [blockSelf checkLogin:parDic];    
    };
    
    loinView.registeblock=^(){
        //跳转到注册页面
        NSLog(@"跳转到注册页面");
         RegisterViewController* registVC=[[RegisterViewController alloc]init];
        UINavigationController* nav=[[UINavigationController alloc]initWithRootViewController:registVC];
        [blockSelf presentViewController:nav animated:YES completion:^{
            
        }];
    };
    
}

-(void)checkLogin:(NSDictionary*)parDic
{
    __block LoginViewController* blockSelf=self;
    if (((NSString*)[parDic objectForKey:@"userName"]).length==0||([(NSString*)[parDic objectForKey:@"userName"] isEqual:@""])) {
        [self qq_performSVHUDBlock:^{
            [SVProgressHUD showErrorWithStatus:@"用户名不能为空！"];
        }];
    }else if(((NSString*)[parDic objectForKey:@"passWord"]).length==0||([(NSString*)[parDic objectForKey:@"passWord"] isEqual:@""]))
    {
        [self qq_performSVHUDBlock:^{
            [SVProgressHUD showErrorWithStatus:@"密码不能为空！"];
        }];
    }else
    {
        [[QQRequestManager sharedRequestManager] GET_loginWithParameters:parDic success:^(NSURLSessionDataTask *task, id responseObject) {
            //NSLog(@"判断是否登录成功跳转界面");
            int status=[(NSNumber*)[responseObject objectForKey:@"status"] intValue];
            if (status==1) {
                //跳转代码
                [blockSelf qq_performSVHUDBlock:^{
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD showSuccessWithStatus:[responseObject objectForKey:@"message"]];
                }];
                [[QQRequestManager sharedRequestManager] SET_userInfo:[responseObject objectForKey:@"user"]];
                ChooseViewController* chooseVC=[[ChooseViewController alloc]init];
                UINavigationController* nav=[[UINavigationController alloc]initWithRootViewController:chooseVC];
                [blockSelf presentViewController:nav animated:YES completion:^{
                }];
                
            }else
            {
                [blockSelf qq_performSVHUDBlock:^{
                    [SVProgressHUD showSuccessWithStatus:[responseObject objectForKey:@"message"]];
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD dismissWithDelay:1.0];
                }];
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            [blockSelf qq_performSVHUDBlock:^{
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD showErrorWithStatus:@"网络请求错误"];
                [SVProgressHUD dismissWithDelay:1.0];
            }];
        }];
        
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
